package com.wesleyxie.mobile.android.todopomodoro.provider;

import static com.wesleyxie.mobile.android.todopomodoro.util.LogUtils.LOGV;
import static com.wesleyxie.mobile.android.todopomodoro.util.LogUtils.makeLogTag;

import java.util.Arrays;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class TodoProvider extends ContentProvider {
	private static final String TAG = makeLogTag(TodoProvider.class);

	private TodoDatabase mOpenHelper;

	private static final UriMatcher sUriMatcher = buildUriMatcher();

	private static final int TASKS = 100;
	private static final int TASKS_ID = 101;

	private static final int LISTS = 200;
	private static final int LISTS_ID = 201;
	
	/**
     * Build and return a {@link UriMatcher} that catches all {@link Uri}
     * variations supported by this {@link ContentProvider}.
     */
    private static UriMatcher buildUriMatcher() {
        final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
        final String authority = TodoContract.CONTENT_AUTHORITY;

        matcher.addURI(authority, "tasks", TASKS);
        matcher.addURI(authority, "tasks/*", TASKS_ID);

        matcher.addURI(authority, "lists", LISTS);
        matcher.addURI(authority, "lists/*", LISTS_ID);

        return matcher;
    }

	@Override
	public boolean onCreate() {
        mOpenHelper = new TodoDatabase(getContext());
        return true;
	}
	
	private void deleteDatabase() {
        // TODO: wait for content provider operations to finish, then tear down
        mOpenHelper.close();
        Context context = getContext();
        TodoDatabase.deleteDatabase(context);
        mOpenHelper = new TodoDatabase(getContext());
    }
	
	@Override
	public String getType(Uri uri) {
		final int match = sUriMatcher.match(uri);
       /* switch (match) {
            case TASKS:
                return Tasks.CONTENT_TYPE;
            case TASKS_ID:
                return Tasks.CONTENT_ITEM_TYPE;
            case LISTS:
                return Lists.CONTENT_TYPE;
            case LISTS_ID:
                return Lists.CONTENT_ITEM_TYPE;
            default:
                throw new UnsupportedOperationException("Unknown uri: " + uri);
        }*/
		
		return null;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		LOGV(TAG, "query(uri=" + uri + ", proj=" + Arrays.toString(projection) + ")");
        final SQLiteDatabase db = mOpenHelper.getReadableDatabase();

        /*final int match = sUriMatcher.match(uri);
        switch (match) {
            default: {
                // Most cases are handled with simple SelectionBuilder
                final SelectionBuilder builder = buildExpandedSelection(uri, match);
                return builder.where(selection, selectionArgs).query(db, projection, sortOrder);
            }
            case SEARCH_SUGGEST: {
                final SelectionBuilder builder = new SelectionBuilder();

                // Adjust incoming query to become SQL text match
                selectionArgs[0] = selectionArgs[0] + "%";
                builder.table(Tables.SEARCH_SUGGEST);
                builder.where(selection, selectionArgs);
                builder.map(SearchManager.SUGGEST_COLUMN_QUERY,
                        SearchManager.SUGGEST_COLUMN_TEXT_1);

                projection = new String[] {
                        BaseColumns._ID,
                        SearchManager.SUGGEST_COLUMN_TEXT_1,
                        SearchManager.SUGGEST_COLUMN_QUERY
                };

                final String limit = uri.getQueryParameter(SearchManager.SUGGEST_PARAMETER_LIMIT);
                return builder.query(db, projection, null, null, SearchSuggest.DEFAULT_SORT, limit);
            }
        }*/
        
        return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		return null;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		return 0;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		return 0;
	}

}
